//
//  eeuiUmengShareModule.m
//

#import "eeuiUmengShareModule.h"
#import "eeuiUmengManager.h"
#import <UMCommon/MobClick.h>
#import <WeexPluginLoader/WeexPluginLoader.h>

#import <UMShare/UMShare.h>
#import <UShareUI/UShareUI.h>

@implementation eeuiUmengShareModule

WX_PlUGIN_EXPORT_MODULE(eeuiUmengShare, eeuiUmengShareModule)

WX_EXPORT_METHOD(@selector(webShare:callback:))

//网页分享
- (void)webShare:(NSString *)options callback:(WXModuleKeepAliveCallback)callback
{
    NSError *err;
    NSMutableDictionary *dic = nil;
    if ([options isKindOfClass:[NSDictionary class]]) {
        dic = (NSMutableDictionary *)options;
    }else{
        NSData *jsonData = [options dataUsingEncoding:NSUTF8StringEncoding];
        dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
    }


    //创建分享消息对象
    UMSocialMessageObject *messageObject =[UMSocialMessageObject messageObject];

    //创建网页内容对象
    UMShareWebpageObject *shareObject =[UMShareWebpageObject shareObjectWithTitle:[WXConvert NSString:dic[@"title"]] descr:[WXConvert NSString:dic[@"description"]] thumImage:[UIImage imageNamed:[WXConvert NSString:dic[@"thumb"]]]];

    //设置网页地址
    shareObject.webpageUrl = [WXConvert NSString:dic[@"url"]];

    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;

    WXModuleKeepAliveCallback weixinCallback = callback;
     //调用分享接口
        [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatSession messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
            if (error) {
                if (callback != nil) {
                    callback(@{@"type":@"error", @"msg":@"分享失败"}, NO);
                }
            }else{
                if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                    if (callback != nil) {
                        callback(@{@"type":@"success", @"msg":@"分享成功"}, NO);
                    }
                }else{
                    if (callback != nil) {
                        callback(@{@"type":@"error", @"msg":@"取消分享"}, NO);
                    }
                    NSLog(@"response data is %@",data);
                }
            }
        }];
}

@end
